using imitateMacQQ.WebApi.Configures;
using imitateMacQQ.WebApi.Domain.Friend.module.Entity.dao;
using imitateMacQQ.WebApi.Domain.Friend.module.Interface;
using imitateMacQQ.WebApi.Domain.Request.utils;
using imitateMacQQ.WebApi.Repository.EFCore.Model;
using Microsoft.EntityFrameworkCore;

namespace imitateMacQQ.WebApi.Domain.Friend.module.Service;

public class GetMesService : IGetFriendMes
{
    public async Task<Request<FriendMes>> GetDimMes(string account)
    {
        try
        {
            await using DataDbContext db = new DataDbContext();
            var info = await db.Users.FirstOrDefaultAsync(
                e => e.Account == account);
            if (info is null)
                throw new Exception("Account Is Null");
            var res = new FriendMes()
            {
                Header = info.Header,
                Name = info.UserName,
                Account = info.Account
            };
            return Request<FriendMes>.Ok(res);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<FriendMes>.Ok(null!, false, ex.Message);
        }
    }

    public async Task<Request<FriendAllMes>> GetAllMes(string account)
    {
        try
        {
            FriendAllMes res;
            await using DataDbContext db = new DataDbContext();
            var info = await db.Users.FirstOrDefaultAsync(e =>
                e.Account == account);
            if (info is null)
                throw new Exception("User is Del");
            if (info.Show)
            {
                res = new FriendAllMes()
                {
                    Account = info.Account,
                    Day = info.Birthday,
                    Header = info.Header,
                    Intro = info.Intro,
                    Live = info.Live,
                    Sex = info.Sex,
                    Phone = info.Phone,
                    State = info.State,
                    Name = info.UserName
                };
            }
            else
            {
                res = new FriendAllMes()
                {
                    Account = info.Account,
                    Day = info.Birthday,
                    Header = info.Header,
                    Intro = "*********",
                    Live = "*********",
                    Sex = "*",
                    Phone = "***********",
                    State = info.State,
                    Name = info.UserName
                };
            }
            
            return Request<FriendAllMes>.Ok(res);
        }
        catch (Exception ex)
        {
            _logHelper.LogError(ex);
            return Request<FriendAllMes>.Ok(null!, false, ex.Message);
        }
    }

    private readonly INLogHelper _logHelper;

    public GetMesService(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }
}